/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.home.gui;


import com.home.common.Role;
import com.home.logic.ScorecardTypeLogic;
import com.home.mapping.Account;
import com.home.mapping.Agent;
import com.home.mapping.ScorecardType;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;


/**
 *
 * @author vi
 */
public class MainScreen extends javax.swing.JFrame {
    
    
    private Map<Integer, ScorecardType> types = new TreeMap<>();
    private Account accountLogin;
    /**
     * Creates new form Main
     */
    public MainScreen() {
        initComponents();
        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
        setLocationRelativeTo(this);
        preLogin();
        types = new ScorecardTypeLogic().findMapAllScorecardType();
        
        repAvgScore.setVisible(false);
        repDigital.setVisible(false);
        repInbound.setVisible(false);
        repOutbound.setVisible(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        desktopPane = new javax.swing.JDesktopPane();
        jToolBar1 = new javax.swing.JToolBar();
        jLabel1 = new javax.swing.JLabel();
        lblNameLogin = new javax.swing.JLabel();
        filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 0));
        jLabel2 = new javax.swing.JLabel();
        lblTeam = new javax.swing.JLabel();
        filler2 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 0));
        jLabel3 = new javax.swing.JLabel();
        lblVenture = new javax.swing.JLabel();
        mainMenu = new javax.swing.JMenuBar();
        mnSystem = new javax.swing.JMenu();
        mnItemLogin = new javax.swing.JMenuItem();
        mnItemChangePass = new javax.swing.JMenuItem();
        mnItemLogout = new javax.swing.JMenuItem();
        mnItemExit = new javax.swing.JMenuItem();
        mnQA = new javax.swing.JMenu();
        mnItemScorecard = new javax.swing.JMenuItem();
        mnReport = new javax.swing.JMenu();
        repInbound = new javax.swing.JMenuItem();
        repDigital = new javax.swing.JMenuItem();
        repOutbound = new javax.swing.JMenuItem();
        repAvgScore = new javax.swing.JMenuItem();
        mnItemQualityReport = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Scorecard Management System");
        setMinimumSize(new java.awt.Dimension(1024, 768));

        desktopPane.setAutoscrolls(true);
        desktopPane.setDragMode(javax.swing.JDesktopPane.OUTLINE_DRAG_MODE);

        javax.swing.GroupLayout desktopPaneLayout = new javax.swing.GroupLayout(desktopPane);
        desktopPane.setLayout(desktopPaneLayout);
        desktopPaneLayout.setHorizontalGroup(
            desktopPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 656, Short.MAX_VALUE)
        );
        desktopPaneLayout.setVerticalGroup(
            desktopPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 429, Short.MAX_VALUE)
        );

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);
        jToolBar1.setFocusable(false);

        jLabel1.setText("Agent Name : ");
        jToolBar1.add(jLabel1);
        jToolBar1.add(lblNameLogin);
        jToolBar1.add(filler1);

        jLabel2.setText("Team : ");
        jToolBar1.add(jLabel2);
        jToolBar1.add(lblTeam);
        jToolBar1.add(filler2);

        jLabel3.setText("Venture : ");
        jToolBar1.add(jLabel3);
        jToolBar1.add(lblVenture);

        mnSystem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/system.png"))); // NOI18N
        mnSystem.setText("System");

        mnItemLogin.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/login.png"))); // NOI18N
        mnItemLogin.setText("Login");
        mnItemLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnItemLoginActionPerformed(evt);
            }
        });
        mnSystem.add(mnItemLogin);

        mnItemChangePass.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/change_pass.png"))); // NOI18N
        mnItemChangePass.setText("Change Password");
        mnItemChangePass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnItemChangePassActionPerformed(evt);
            }
        });
        mnSystem.add(mnItemChangePass);

        mnItemLogout.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/logout.png"))); // NOI18N
        mnItemLogout.setText("Logout");
        mnItemLogout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnItemLogoutActionPerformed(evt);
            }
        });
        mnSystem.add(mnItemLogout);

        mnItemExit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/exit.png"))); // NOI18N
        mnItemExit.setText("Exit");
        mnItemExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnItemExitActionPerformed(evt);
            }
        });
        mnSystem.add(mnItemExit);

        mainMenu.add(mnSystem);

        mnQA.setText("Quality Assurance");

        mnItemScorecard.setText("Scorecard");
        mnItemScorecard.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnItemScorecardActionPerformed(evt);
            }
        });
        mnQA.add(mnItemScorecard);

        mainMenu.add(mnQA);

        mnReport.setText("Report");

        repInbound.setText("Inbound");
        repInbound.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                repInboundActionPerformed(evt);
            }
        });
        mnReport.add(repInbound);

        repDigital.setText("Digital");
        repDigital.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                repDigitalActionPerformed(evt);
            }
        });
        mnReport.add(repDigital);

        repOutbound.setText("Outbound");
        repOutbound.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                repOutboundActionPerformed(evt);
            }
        });
        mnReport.add(repOutbound);

        repAvgScore.setText("AVG Score");
        repAvgScore.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                repAvgScoreActionPerformed(evt);
            }
        });
        mnReport.add(repAvgScore);

        mnItemQualityReport.setText("Quality Report");
        mnItemQualityReport.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnItemQualityReportActionPerformed(evt);
            }
        });
        mnReport.add(mnItemQualityReport);

        mainMenu.add(mnReport);

        setJMenuBar(mainMenu);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(desktopPane)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(desktopPane)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void mnItemLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnItemLoginActionPerformed
        this.addNewInternalFrame(new Login(this));
    }//GEN-LAST:event_mnItemLoginActionPerformed

    private void mnItemLogoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnItemLogoutActionPerformed
        
        role = -1;
       
        //addNewInternalFrame(new Login(this));
        fillInfoEmployeeLogin("", "", "");
        closeAllWindow();
        preLogin();
        this.setAccountLogin(null);
        this.setAgentLogin(null);
        this.setRole(-1);
        
    }//GEN-LAST:event_mnItemLogoutActionPerformed

    private void mnItemExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnItemExitActionPerformed
          int r = JOptionPane.showConfirmDialog(null, "Cancel process?", "Notify", JOptionPane.OK_CANCEL_OPTION);
        if (r == JOptionPane.OK_OPTION) {
                 
                System.exit(0);
        }
    }//GEN-LAST:event_mnItemExitActionPerformed

    private void mnItemScorecardActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnItemScorecardActionPerformed
        final MainScreen main = this;
        final WaitingDialog loadingDialog = new WaitingDialog(this, true);
        SwingWorker<?, ?> worker = new SwingWorker<Void, Void>() {
            @Override
            protected Void doInBackground() throws Exception {
                addNewInternalFrame(new QAWorkingList(main));
                return null;
            }

            @Override
            protected void done() {
                loadingDialog.dispose();
            }
        };
        worker.execute();
        loadingDialog.setVisible(true);
                     
    }//GEN-LAST:event_mnItemScorecardActionPerformed

    private void repInboundActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_repInboundActionPerformed
        final ScorecardType type = types.get(1);
        final WaitingDialog loadingDialog = new WaitingDialog(this, true);
        SwingWorker<?, ?> worker = new SwingWorker<Void, Void>() {
            @Override
            protected Void doInBackground() throws Exception {
                addNewInternalFrame(new InboundReport(type));
                return null;
            }

            @Override
            protected void done() {
                loadingDialog.dispose();
            }
        };
        worker.execute();
        loadingDialog.setVisible(true);


    }//GEN-LAST:event_repInboundActionPerformed

    private void repDigitalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_repDigitalActionPerformed
        final ScorecardType type = types.get(2);
        final WaitingDialog loadingDialog = new WaitingDialog(this, true);
        SwingWorker<?, ?> worker = new SwingWorker<Void, Void>() {
            @Override
            protected Void doInBackground() throws Exception {
                addNewInternalFrame(new InboundReport(type));
                return null;
            }

            @Override
            protected void done() {
                loadingDialog.dispose();
            }
        };
        worker.execute();
        loadingDialog.setVisible(true);
    }//GEN-LAST:event_repDigitalActionPerformed

    private void repOutboundActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_repOutboundActionPerformed
        final ScorecardType type = types.get(3);
        final WaitingDialog loadingDialog = new WaitingDialog(this, true);
        SwingWorker<?, ?> worker = new SwingWorker<Void, Void>() {
            @Override
            protected Void doInBackground() throws Exception {
                addNewInternalFrame(new InboundReport(type));
                return null;
            }

            @Override
            protected void done() {
                loadingDialog.dispose();
            }
        };
        worker.execute();
        loadingDialog.setVisible(true);
    }//GEN-LAST:event_repOutboundActionPerformed

    private void repAvgScoreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_repAvgScoreActionPerformed
        final ScorecardType type = types.get(1);
        final WaitingDialog loadingDialog = new WaitingDialog(this, true);
        SwingWorker<?, ?> worker = new SwingWorker<Void, Void>() {
            @Override
            protected Void doInBackground() throws Exception {
                addNewInternalFrame(new AVGScoreReport(type));
                return null;
            }

            @Override
            protected void done() {
                loadingDialog.dispose();
            }
        };
        worker.execute();
        loadingDialog.setVisible(true);
    }//GEN-LAST:event_repAvgScoreActionPerformed

    private void mnItemChangePassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnItemChangePassActionPerformed
        addNewInternalFrame(new ChangePassword(this));
    }//GEN-LAST:event_mnItemChangePassActionPerformed

    private void mnItemQualityReportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnItemQualityReportActionPerformed
        addNewInternalFrame(new QualityReport(this));
    }//GEN-LAST:event_mnItemQualityReportActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainScreen().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JDesktopPane desktopPane;
    private javax.swing.Box.Filler filler1;
    private javax.swing.Box.Filler filler2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JLabel lblNameLogin;
    private javax.swing.JLabel lblTeam;
    private javax.swing.JLabel lblVenture;
    private javax.swing.JMenuBar mainMenu;
    private javax.swing.JMenuItem mnItemChangePass;
    private javax.swing.JMenuItem mnItemExit;
    private javax.swing.JMenuItem mnItemLogin;
    private javax.swing.JMenuItem mnItemLogout;
    private javax.swing.JMenuItem mnItemQualityReport;
    private javax.swing.JMenuItem mnItemScorecard;
    private javax.swing.JMenu mnQA;
    private javax.swing.JMenu mnReport;
    private javax.swing.JMenu mnSystem;
    private javax.swing.JMenuItem repAvgScore;
    private javax.swing.JMenuItem repDigital;
    private javax.swing.JMenuItem repInbound;
    private javax.swing.JMenuItem repOutbound;
    // End of variables declaration//GEN-END:variables
    
    private int role;
    private Agent agentLogin;
    public void addNewInternalFrame(JInternalFrame frame) {
        if (frame != null) {
            if (checkWindowAvailable(frame)) {
                desktopPane.add(frame);
                frame.setVisible(true);
            } else {
                return;
            }
        }
    }
    
    //check window has been available ?
    private boolean checkWindowAvailable(JInternalFrame window) {
        for (JInternalFrame inFrame : desktopPane.getAllFrames()) {
	if (inFrame.getTitle() == window.getTitle()) {
                return false;
            }
        }
            return true;
    }
    // reomve specific interframe
    public void removeAFrame(JInternalFrame frame) {
        for (JInternalFrame inFrame : desktopPane.getAllFrames()) {
            if (inFrame.getTitle() == frame.getTitle()) {
                desktopPane.remove(inFrame);
            }
        }
    }
    // close all window
    private void closeAllWindow() {
        for (JInternalFrame inFrame : desktopPane.getAllFrames()) {
            inFrame.dispose();
        }
    }
    
    public void fillInfoEmployeeLogin(String name, String departnament, String position) {
        lblNameLogin.setText(name);
        lblVenture.setText(departnament);
        lblTeam.setText(position);
    }

    private void preLogin() {
        fillInfo("", "", "");
        mnItemLogout.setEnabled(false);
        mnQA.setVisible(false);
        mnReport.setVisible(false);
        mnItemChangePass.setEnabled(false);
        mnItemLogout.setEnabled(false);
        mnItemLogin.setEnabled(true);
    }
    
    public void afterLogin() {
        mnItemChangePass.setEnabled(true);
        mnItemLogout.setEnabled(true);
        mnItemLogin.setEnabled(false);
        if (role == Role.ROLE_QA) {
            mnQA.setVisible(true);
            mnReport.setVisible(true);
        }
    }
    
    public void fillInfo(String name, String team, String venture) {
        lblNameLogin.setText(name);
        lblTeam.setText(team);
        lblVenture.setText(venture);
    }
    
    public Agent getAgentLogin() {
        return agentLogin;
    }

    public void setAgentLogin(Agent agentLogin) {
        this.agentLogin = agentLogin;
    }

    public int getRole() {
        return role;
    }

    public void setRole(int role) {
        this.role = role;
    }

    public Account getAccountLogin() {
        return accountLogin;
    }

    public void setAccountLogin(Account accountLogin) {
        this.accountLogin = accountLogin;
    }
    
    
}
